/*********************************************************************\
*
*                  (c) lkc
*                  All Rights Reserved. lkc Confidential
*
\*********************************************************************/
/*********************************************************************\
*   Title                      :
*
*   Module Description         :
*
*   Author                     : lkc
*
*   Created                    : 2018-7-24
*
\*********************************************************************/

#include <unistd.h>
#include <memory.h>
#include "CommBasePosix.h"


int CommBasePosix::GetFd()
{
    return m_fd;
}

void CommBasePosix::SetFd(int fd)
{
    m_fd = fd;
}

void CommBasePosix::Close()
{
    if(m_fd<0)
    {
        printf("no fd to close\n");
        return;
    }
    int ret = close(m_fd);
    if(ret>=0)
    {
        printf("close fd success\n");
        m_fd = -1;
    }
}

CommBasePosix::~CommBasePosix()
{
    printf("CommBasePosix derived() is called! Then close fd\n");
    Close();
}

int CommBasePosix::ReadAndParse()//只能跟epoll之类的方式配合使用,因为是通过epoll触发的,while中判断 errno == EAGAIN后调用EpollSetOneshot( epollFd, fd )
{
    int BUFFER_SIZE = 1024;
    unsigned char buf[BUFFER_SIZE];
    memset(buf,0,BUFFER_SIZE);
    int ret = Read(buf,BUFFER_SIZE);
    if(ret<=0)
    {
        return ret;
    }
    if(ExtractFun != nullptr)
    {
        ExtractFun(buf,ret);
    }
    return ret;
}

/********************************************************************\
 *
 * REVISION RECORDS
 *
\*********************************************************************/
/*********************************************************************/
/*
 *
 *
 *
\*********************************************************************/
/*------------------------------The End------------------------------*/
